﻿using BaseData.CommonLib.Constants;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using BaseData.ApplicationLib.Commands;
using BaseData.ApplicationLib.Queries;
using BaseData.CommonLib.Models;

namespace BaseData.WebApi.Controllers;

[ApiController]
public class WarehouseController : ControllerBase
{
    private readonly IMediator _mediator;
    private readonly IWarehouseQueries _processQueries;

    public WarehouseController(IMediator mediator,
        IWarehouseQueries processQueries)
    {
        _mediator = mediator;
        _processQueries = processQueries;
    }

    [HttpGet(ApiRoutes.Warehouse.QueryWarehouses)]
    public async Task<IActionResult> Get([FromQuery] WarehouseSearchRequestDto model)
    {
        if (model.Page > 0 || model.Limit > 0)
        {
            var data = await _processQueries.QueryWarehousesByPage(model);
            return Ok(data);
        }
        else
        {
            var data = await _processQueries.QueryWarehouses(model);
            return Ok(data);
        }
    }

    [HttpPost(ApiRoutes.Warehouse.CreateWarehouse)]
    public async Task<IActionResult> Post(CreateWarehouseCommand command)
    {
        var result = await _mediator.Send(command);
        return Ok(result);
    }

    [HttpGet(ApiRoutes.Warehouse.GetWarehouse)]
    public async Task<IActionResult> Get(int id)
    {
        var result = await _processQueries.GetWarehouse(id);
        return Ok(result);
    }

    [HttpPut(ApiRoutes.Warehouse.UpdateWarehouse)]
    public async Task<IActionResult> Put(int id, UpdateWarehouseCommand command)
    {
        command.Id = id;
        var result = await _mediator.Send(command);
        return Ok(result);
    }
}